﻿/*
 * Author: M.Aldworth of InfiniteDesigns.org
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Security;
using System.Security.Permissions;

namespace SVNCleaner
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                //If no arguments exist, don't continue
                if (args.Length != 1)
                {
                    Console.WriteLine("Please have only 1 argument, the directory path.");
                    return;
                }

                //If the directory doesn't exist, don't continue
                if (!Directory.Exists(args[0]))
                {
                    Console.WriteLine("The argument provided wasn't a valid directory");
                    return;
                }

                //Recursively gets all files within the given directory, to be used to change any files that may be ReadOnly, to Normal
                string[] fileResults = Directory.GetFiles(args[0], "*", SearchOption.AllDirectories);

                //For some wierd reason all-wcprops couldn't would throw UnauthorizedAccessException when the Directory.Delete was being executed.
                //After changing all ReadOnly files to Normal, it worked like a charm.
                foreach (string file in fileResults)
                {
                    //Normally this would change every file within the project to be Normal. We don't want this as some users may have non .svn files that are ReadOnly
                    //We execute this if clause.
                    if (file.Contains("\\.svn"))
                        File.SetAttributes(file, FileAttributes.Normal);
                }

                //Finally recursively gets all directories marked as .svn
                string[] dirResults = Directory.GetDirectories(args[0], "*.svn", SearchOption.AllDirectories);

                foreach (string dir in dirResults)
                {
                    if (Directory.Exists(dir))
                        Directory.Delete(dir, true);
                }

                Console.WriteLine("Successfully removed all .svn folders from directory");
            }
            //Output all exceptions to a text file, for debugging if necessary.
            catch (Exception e)
            {
                string fileName = args[0].Substring(args[0].LastIndexOf('\\')) + ".txt";

                if (!Directory.Exists(Environment.CurrentDirectory+"\\Logs"))
                    Directory.CreateDirectory(Environment.CurrentDirectory + "\\Logs");

                StreamWriter sw = new StreamWriter(Environment.CurrentDirectory + "\\Logs\\" + fileName, true);

                sw.WriteLine(Environment.NewLine + DateTime.Now.ToString());
                sw.WriteLine("Message: "+e.Message);
                sw.WriteLine("Source: "+e.Source);
                sw.WriteLine("StackTrace: " + e.StackTrace);
                sw.Close();
            }

        }
    }
}
